package com.dsbj.mp.icardservice.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsbj.common.msg.ResponseMessage;
import com.dsbj.mp.icardservice.entity.MpRole;
import com.dsbj.mp.icardservice.entity.MpUserRole;
import com.dsbj.mp.icardservice.mapper.MpRoleMapper;
import com.dsbj.mp.icardservice.mapper.MpUserRoleMapper;
import com.dsbj.mp.icardservice.utils.Constants;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * "用户角色分配"的服务类。
 */
@Service
@Transactional
public class MpUserRoleService extends ServiceImpl<MpUserRoleMapper, MpUserRole> {
	
	@Resource
	private MpUserRoleMapper mpUserRoleMapper;
	
	@Resource
	private MpRoleMapper mpRoleMapper;
	
	@Resource
	private MpRoleService mpRoleService;
	
	
	/**
	 * 根据idcardno获取该员工的角色列表。
	 * @param
	 * @return
	 * @author cjw 2020-03-03
	 */
	public List<MpUserRole> getMpUserRoleByIdcardno(String idcardno){
		QueryWrapper<MpUserRole> queryWrapper=new QueryWrapper<MpUserRole>()
				.eq("IDCARDNO",idcardno);
		List<MpUserRole> list_mpUserRole=mpUserRoleMapper.selectList(queryWrapper);
		return list_mpUserRole;
	}
	
	/**
	 * 向员工分配"普通员工角色",若已分配该角色,则不处理。
	 * @param
	 * @return
	 * @author cjw 2020-03-02
	 */
	public String assignRoleToCommonEmployee(String idcardno){
		//获取"普通员工角色"
		String roleName="普通员工";
		MpRole mpRole=mpRoleService.getMpRoleByName(roleName);
		if(mpRole==null){
			return Constants.ERROR;
		}
		
		//获取该员工的角色信息,分析是否已分配该角色。
		List<MpUserRole> list_mpUserRole= getMpUserRoleByIdcardno(idcardno);
		for(MpUserRole mpUserRole: list_mpUserRole){
			if(mpRole.getId().equals(mpUserRole.getRoleId())){
				return Constants.SUCCESS;
			}
		}
		
		//向该员工分配该角色
		MpUserRole mpUserRole=new MpUserRole();
		mpUserRole.setIdcardno(idcardno);
		mpUserRole.setRoleId(mpRole.getId());
		mpUserRole.setKeyIdentity(idcardno);
		mpUserRoleMapper.insert(mpUserRole);
		return Constants.SUCCESS;
	}
	
	
	
	

}
